{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create MBTiles for Cerrado cropland and pasture areas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import geopandas as gpd\n",
    "import matplotlib.pyplot as plt\n",
    "import subprocess"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_mbtiles(source_path, dest_path, layer_name, opts=\"-zg --drop-densest-as-needed --extend-zooms-if-still-dropping --force --read-parallel\"):\n",
    "    \"\"\"\n",
    "    Use tippecanoe to create a MBTILE at dest_path from source_path.\n",
    "    layer_name is used for the name of the layer in the MBTILE.\n",
    "    Regex file path (/*.geojson) is supported for source_path.\n",
    "    \"\"\"\n",
    "    cmd = f\"tippecanoe -o {dest_path} -l {layer_name} {opts} {source_path}\"\n",
    "    print(f\"Processing: {cmd}\")\n",
    "    r = subprocess.call(cmd, shell=True)\n",
    "    if r == 0:\n",
    "        print(\"Task created\")\n",
    "    else:\n",
    "        print(\"Task failed\")\n",
    "    print(\"Finished processing\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Croplands"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf = gpd.read_file('../data/agricultura_agrosatelite/agricultura_agrosatelite_2001')\n",
    "gdf.to_file('../data/agricultura_agrosatelite/agricultura_agrosatelite_2001.json', driver=\"GeoJSON\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Year: 2007\n",
      "Processing: tippecanoe -o ../data/agricultura_agrosatelite/agricultura_agrosatelite_2007.mbtiles -l agricultura_agrosatelite_2007 -zg --drop-densest-as-needed --extend-zooms-if-still-dropping --force --read-parallel ../data/agricultura_agrosatelite/agricultura_agrosatelite_2007.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2014\n",
      "Processing: tippecanoe -o ../data/agricultura_agrosatelite/agricultura_agrosatelite_2014.mbtiles -l agricultura_agrosatelite_2014 -zg --drop-densest-as-needed --extend-zooms-if-still-dropping --force --read-parallel ../data/agricultura_agrosatelite/agricultura_agrosatelite_2014.json\n",
      "Task created\n",
      "Finished processing\n"
     ]
    }
   ],
   "source": [
    "for year in ['2001', '2007', '2014']:\n",
    "    print(f'Year: {year}')\n",
    "    gdf = gpd.read_file(f'../data/agricultura_agrosatelite/agricultura_agrosatelite_{year}')\n",
    "    gdf.to_file(f'../data/agricultura_agrosatelite/agricultura_agrosatelite_{year}.json', driver=\"GeoJSON\")\n",
    "    \n",
    "    layer_name = f'agricultura_agrosatelite_{year}'\n",
    "    source_path = f'../data/agricultura_agrosatelite/agricultura_agrosatelite_{year}.json'\n",
    "    dest_path = f'../data/agricultura_agrosatelite/agricultura_agrosatelite_{year}.mbtiles'\n",
    "    create_mbtiles(source_path, dest_path, layer_name, opts=\"-zg --drop-densest-as-needed --extend-zooms-if-still-dropping --force --read-parallel\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Listening on http://localhost:9000\n",
      "^C\n"
     ]
    }
   ],
   "source": [
    "!mbview --port 9000 ../data/agricultura_agrosatelite/agricultura_agrosatelite_2014.mbtiles"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pastures"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Year: 1985\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_1985.mbtiles -l pasture_cerrado_1985 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_1985.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 1986\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_1986.mbtiles -l pasture_cerrado_1986 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_1986.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 1987\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_1987.mbtiles -l pasture_cerrado_1987 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_1987.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 1988\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_1988.mbtiles -l pasture_cerrado_1988 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_1988.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 1989\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_1989.mbtiles -l pasture_cerrado_1989 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_1989.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 1990\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_1990.mbtiles -l pasture_cerrado_1990 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_1990.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 1991\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_1991.mbtiles -l pasture_cerrado_1991 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_1991.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 1992\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_1992.mbtiles -l pasture_cerrado_1992 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_1992.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 1993\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_1993.mbtiles -l pasture_cerrado_1993 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_1993.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 1994\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_1994.mbtiles -l pasture_cerrado_1994 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_1994.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 1995\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_1995.mbtiles -l pasture_cerrado_1995 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_1995.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 1996\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_1996.mbtiles -l pasture_cerrado_1996 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_1996.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 1997\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_1997.mbtiles -l pasture_cerrado_1997 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_1997.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 1998\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_1998.mbtiles -l pasture_cerrado_1998 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_1998.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 1999\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_1999.mbtiles -l pasture_cerrado_1999 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_1999.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2000\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2000.mbtiles -l pasture_cerrado_2000 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2000.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2001\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2001.mbtiles -l pasture_cerrado_2001 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2001.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2002\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2002.mbtiles -l pasture_cerrado_2002 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2002.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2003\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2003.mbtiles -l pasture_cerrado_2003 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2003.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2004\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2004.mbtiles -l pasture_cerrado_2004 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2004.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2005\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2005.mbtiles -l pasture_cerrado_2005 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2005.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2006\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2006.mbtiles -l pasture_cerrado_2006 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2006.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2007\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2007.mbtiles -l pasture_cerrado_2007 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2007.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2008\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2008.mbtiles -l pasture_cerrado_2008 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2008.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2009\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2009.mbtiles -l pasture_cerrado_2009 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2009.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2010\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2010.mbtiles -l pasture_cerrado_2010 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2010.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2011\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2011.mbtiles -l pasture_cerrado_2011 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2011.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2012\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2012.mbtiles -l pasture_cerrado_2012 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2012.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2013\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2013.mbtiles -l pasture_cerrado_2013 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2013.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2014\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2014.mbtiles -l pasture_cerrado_2014 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2014.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2015\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2015.mbtiles -l pasture_cerrado_2015 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2015.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2016\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2016.mbtiles -l pasture_cerrado_2016 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2016.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2017\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2017.mbtiles -l pasture_cerrado_2017 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2017.json\n",
      "Task created\n",
      "Finished processing\n",
      "Year: 2018\n",
      "Processing: tippecanoe -o ../data/pasture_cerrado/pasture_cerrado_2018.mbtiles -l pasture_cerrado_2018 -zg --force --read-parallel ../data/pasture_cerrado/pasture_cerrado_2018.json\n",
      "Task created\n",
      "Finished processing\n"
     ]
    }
   ],
   "source": [
    "for year in np.arange(1985,2019).astype(str):\n",
    "    print(f'Year: {year}')\n",
    "    gdf = gpd.read_file(f'../data/pasture_cerrado/pasture_cerrado_{year}/pasture_cerrado_{year}.shp')\n",
    "    gdf.to_file(f'../data/pasture_cerrado/pasture_cerrado_{year}.json', driver=\"GeoJSON\")\n",
    "    \n",
    "    layer_name = f'pasture_cerrado_{year}'\n",
    "    source_path = f'../data/pasture_cerrado/pasture_cerrado_{year}.json'\n",
    "    dest_path = f'../data/pasture_cerrado/pasture_cerrado_{year}.mbtiles'\n",
    "    create_mbtiles(source_path, dest_path, layer_name, opts=\"-zg --force --read-parallel\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/bin/bash: mbview: command not found\n"
     ]
    }
   ],
   "source": [
    "!mbview --port 9000 ../data/pasture_cerrado/pasture_cerrado_2018.mbtiles"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
